﻿using System.Collections.Generic;
using System.Linq;

namespace SweetSurvey.Models.Repositories
{
    public class ParticipatorRepository: IParticipatorRepository
    {
        #region IRepository<Participator,string> Members

        public Participator Get(string id, bool detaching = true)
        {
            var context = new SweetSurveyModelContainer();
            var entity = context.Participators.Where(e => e.Id == id).Single();
            if (detaching) { context.Detach(entity); }
            return entity;
        }

        public string Create(Participator entity)
        {
            if (string.IsNullOrEmpty(entity.Id))
                entity.Id = RepositoryBase.GenerateRandomId();
            var context = new SweetSurveyModelContainer();
            context.Participators.AddObject(entity);
            context.SaveChanges();
            return entity.Id;
        }

        public void Update(Participator entity)
        {
            RepositoryBase.Update(entity);
        }

        public void Delete(string id)
        {
            RepositoryBase.Delete(Get(id));
        }

        #endregion

        #region IParticipatorRepository Members

        public int GetParticipatorCountBySurveyId(string id)
        {
            var context = new SweetSurveyModelContainer();
            var options = new HashSet<string>();
            var questions = context.Questions.Where(q => q.SurveyId == id);
            foreach (var q in questions)
            {
                foreach (var o in q.Options) { options.Add(o.Id); }
            }
            var answers = context.Answers.Where(a => options.Contains(a.OptionId));
            var participants = new HashSet<string>();
            foreach (var a in answers)
            {
                if (!participants.Contains(a.ParticipatorId))
                    participants.Add(a.ParticipatorId);
            }
            return participants.Count;
        }

        public IQueryable<ParticipatorModel> GetAllParticipators()
        {
            var context = new SweetSurveyModelContainer();
            var results = from p in context.Participators
                          join a in context.Answers on p.Id equals a.ParticipatorId
                          join o in context.Options on a.OptionId equals o.Id
                          join q in context.Questions on o.QuestionId equals q.Id
                          join s in context.Surveys on q.SurveyId equals s.Id
                          group p
                          by new
                          {
                              ParticipatorId = p.Id,
                              ParticipatorDate = p.Date,
                              SurveyId = s.Id,
                              SurveyTitle = s.Title
                          }
                          into participator
                          orderby participator.Key.ParticipatorDate descending
                          select new ParticipatorModel()
                          {
                               ParticipatorId = participator.Key.ParticipatorId,
                               ParticipatorDate = participator.Key.ParticipatorDate,
                               SurveyId = participator.Key.SurveyId,
                               SurveyTitle = participator.Key.SurveyTitle
                          };
            return results;
        }

        #endregion
    }
}